Option Strict On

Imports System
Imports System.Web.UI
Imports TagLib

Partial Class Controls_Tagging_TagCloud
    Inherits UserControl

    Private mIdentifier As Guid
      

    Public Property DocumentIdentifier() As Guid
        Get
            If mIdentifier = Guid.Empty AndAlso (ViewState("mIdentifier") IsNot Nothing OrElse Request.QueryString("Id") IsNot Nothing) Then
                If ViewState("mIdentifier") IsNot Nothing Then
                    mIdentifier = CType(ViewState("mIdentifier"), Guid)
                Else
                    ViewState("mIdentifier") = New Guid(Request.QueryString("Id"))
                End If
            End If
            Return mIdentifier
        End Get
        Set(ByVal value As Guid)
            If ViewState("mIdentifier") IsNot Nothing Then Return
            mIdentifier = value
            ViewState("mIdentifier") = value
            Me.Visible = True
            Me.Bind()
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If Context.User.Identity.IsAuthenticated Then
            Me.pnlLoggedIn.Visible = True
            Me.pnlLoggedOut.Visible = False
        Else
            Me.pnlLoggedIn.Visible = False
            Me.pnlLoggedOut.Visible = True
        End If

        If Page.IsPostBack Then Return
        ' Get Tag Cloud
        'Dim Id As String = Request.QueryString("Id")
        'If Id IsNot Nothing AndAlso Id.Length = 36 Then
        '    Bind()
        'Else
        '    If mIdentifier = Guid.Empty Then
        '        Me.Visible = False
        '    End If
        'End If
    End Sub

    Private Sub Bind()
        'If Me.TagCloud.Controls.Count > 0 Then Return
        Me.TagCloud.DataSource = Tagging.GetDocumentTagCloud(Me.DocumentIdentifier)
        Me.TagCloud.DataBind()
    End Sub

    Protected Sub btnAddTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddTag.Click
        Dim user As String
        If Context.User.Identity.IsAuthenticated Then
            user = Context.User.Identity.Name
        Else
            user = Request.UserHostAddress
        End If
        Dim tagging As New Tagging
        tagging.TagDocument(txtTags.Text, Me.DocumentIdentifier, user)
        Bind()
    End Sub

End Class
